'''
Created on Sep 20, 2011

@author: haddaway
'''
  
import Logger, Config, CommunicationManager, PacketManager, DeviceManager
from multiprocessing import Queue

config = Config.config
packetQueue = Queue()
packetManager = PacketManager.PacketManager()
deviceManager = DeviceManager.DeviceManager()

# Initialize logger
logger = Logger.logger.mainLogger
logger.log(1, 'Logger initialized successfully.')

communicationManager = CommunicationManager.CommunicationManager(packetQueue)

def authorizePacket(packet):
    '''
    This is in charge of ensuring packets originate from known device types
    '''
    if (packet.toString() not in config.packetSet):
        if(packet.deviceType in config.deviceTypes):
            packetManager.processPacket(packet, deviceManager, communicationManager)
        else:
            if packet.statusCode not in config.statusTypes:
                status = "Unknown: " + packet.statusCode
            else:
                status = config.statusTypes[packet.statusCode]
            logger.log(3, "Unknown device sends packet: ID: " + packet.deviceID + " Priority: " + packet.priority + " Status: " + status + " Data: " + packet.data)
notFinished = True
while notFinished:
# Start primary command loop
    if not packetQueue.empty():
        currentPacket = packetQueue.get()
        authorizePacket(currentPacket)
        
communicationManager.kill()
logger.log(2, 'Program exiting.')